package cn.edu.nju.ws.sview.wyge.relations.adataset.overview.dataGet;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;

public class typeInfo {
	static int reduce(ArrayList<concept> concepts,
			HashMap<String, Integer> conceptIdToIndex, ArrayList<String> type) {
		ArrayList<Integer> originTypeId = new ArrayList<Integer>();
		ArrayList<Integer> reducedTypeId = new ArrayList<Integer>();
		for (int i = 0; i < type.size(); ++i)
			originTypeId.add(conceptIdToIndex.get(type.get(i)));
		for (int i = 0; i < originTypeId.size(); ++i) {
			boolean find = false;
			int x = originTypeId.get(i);
			for (int j = 0; j < originTypeId.size(); ++j) {
				if (i == j)
					continue;
				int y = originTypeId.get(j);
				if (concepts.get(y).ancestor.contains(x)) {
					find = true;
					break;
				}
			}
			if (!find)
				reducedTypeId.add(x);
		}
		// System.out.println(type.size());
		if (reducedTypeId.size() == 1) {
			return reducedTypeId.get(0);
		}
		String tottype = "";
		for (int i = 0; i < reducedTypeId.size(); ++i)
			tottype += reducedTypeId.get(i) + "qcx";
		if (conceptIdToIndex.containsKey(tottype)) {
			return conceptIdToIndex.get(tottype);
		}
		concept c = new concept();
		c.label = new String();
		for (int i = 0; i < reducedTypeId.size(); ++i) {
			if (i != 0)
				c.label += " and ";
			c.label += concepts.get(reducedTypeId.get(i)).label;
			c.father.add(reducedTypeId.get(i));
			c.ancestor.add(reducedTypeId.get(i));
			Object ob[] = concepts.get(reducedTypeId.get(i)).ancestor.toArray();
			for (int j = 0; j < ob.length; ++j)
				c.ancestor.add((Integer) ob[j]);
		}
		c.uri = c.label;
		concepts.add(c);
		conceptIdToIndex.put(tottype, concepts.size() - 1);
		return concepts.size() - 1;
		// System.out.println(id + " " + tottype);
	}

	public static HashMap<String, Integer> getType(ArrayList<concept> concepts,
			ArrayList<Integer> documents) throws ClassNotFoundException,
			SQLException {
		HashMap<String, Integer> conceptIdToIndex = new HashMap<String, Integer>();
		for (int i = 0; i < concepts.size(); ++i)
			conceptIdToIndex.put('u' + concepts.get(i).id, i);
		HashMap<String, Integer> instanceIdToIndex = new HashMap<String, Integer>();
		Connection con = DBConnectionFactory.getConnection();//.getFalconConnection();
		int typeid = 0;
		try {
			typeid = URIIDCache.getInstance().getURIID(
					"http://www.w3.org/1999/02/22-rdf-syntax-ns#type", false);
		} catch (Throwable e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		for (int i = 0; i < documents.size(); ++i) {
			String query = "SELECT distinct s, o FROM quadruple where p = \"u"+typeid+"\" and doc_id = "
					+ documents.get(i) + " order by s,  o";
			// System.out.println(query);
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(query);
			ArrayList<String> type = new ArrayList<String>();
			String begin = "";
			while (rs.next()) {
				if (begin.compareTo(rs.getString(1)) != 0) {
					begin = rs.getString(1);
					if (type.size() > 0) {
						int index = reduce(concepts, conceptIdToIndex, type);
						instanceIdToIndex.put(begin, index);
						++concepts.get(index).w;
						// System.out.println(i);
						type.clear();
					}
				}
				// System.out.println(begin + " " + rs.getString(1) + " " +
				// rs.getString(2));
				type.add(rs.getString(2));
			}
			if (type.size() > 0) {
				int index = reduce(concepts, conceptIdToIndex, type);
				instanceIdToIndex.put(begin, index);
				++concepts.get(index).w;
			}
			rs.close();
			st.close();
		}
		con.close();
		return instanceIdToIndex;
	}
}
